home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch8 / PlayWait.frm < prev    next >
Text File  |  1999-05-27  |  10KB  |  351 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmPlayWait 
  4.    Caption         =   "PlayWait"
  5.    ClientHeight    =   3825
  6.    ClientLeft      =   1680
  7.    ClientTop       =   975
  8.    ClientWidth     =   5850
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   255
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   390
  14.    Begin VB.TextBox txtNumFrames 
  15.       Height          =   285
  16.       Left            =   1560
  17.       TabIndex        =   10
  18.       Text            =   "100"
  19.       Top             =   120
  20.       Width           =   375
  21.    End
  22.    Begin VB.OptionButton optRunType 
  23.       Caption         =   "Looping"
  24.       Height          =   255
  25.       Index           =   2
  26.       Left            =   360
  27.       TabIndex        =   8
  28.       Top             =   1560
  29.       Width           =   1095
  30.    End
  31.    Begin VB.OptionButton optRunType 
  32.       Caption         =   "Reversing"
  33.       Height          =   255
  34.       Index           =   1
  35.       Left            =   360
  36.       TabIndex        =   7
  37.       Top             =   1200
  38.       Width           =   1095
  39.    End
  40.    Begin VB.OptionButton optRunType 
  41.       Caption         =   "One time"
  42.       Height          =   255
  43.       Index           =   0
  44.       Left            =   360
  45.       TabIndex        =   6
  46.       Top             =   840
  47.       Value           =   -1  'True
  48.       Width           =   1095
  49.    End
  50.    Begin VB.TextBox txtFramesPerSecond 
  51.       Height          =   285
  52.       Left            =   1560
  53.       TabIndex        =   5
  54.       Text            =   "20"
  55.       Top             =   480
  56.       Width           =   375
  57.    End
  58.    Begin VB.PictureBox picFrame 
  59.       AutoRedraw      =   -1  'True
  60.       AutoSize        =   -1  'True
  61.       Height          =   375
  62.       Index           =   0
  63.       Left            =   1560
  64.       ScaleHeight     =   21
  65.       ScaleMode       =   3  'Pixel
  66.       ScaleWidth      =   21
  67.       TabIndex        =   2
  68.       Top             =   1560
  69.       Visible         =   0   'False
  70.       Width           =   375
  71.    End
  72.    Begin VB.CommandButton cmdStart 
  73.       Caption         =   "Start"
  74.       Default         =   -1  'True
  75.       Enabled         =   0   'False
  76.       Height          =   375
  77.       Left            =   600
  78.       TabIndex        =   1
  79.       Top             =   2040
  80.       Width           =   855
  81.    End
  82.    Begin VB.PictureBox picCanvas 
  83.       Height          =   3810
  84.       Left            =   2040
  85.       ScaleHeight     =   250
  86.       ScaleMode       =   3  'Pixel
  87.       ScaleWidth      =   250
  88.       TabIndex        =   0
  89.       Top             =   0
  90.       Width           =   3810
  91.    End
  92.    Begin MSComDlg.CommonDialog dlgOpenFile 
  93.       Left            =   1560
  94.       Top             =   960
  95.       _ExtentX        =   847
  96.       _ExtentY        =   847
  97.       _Version        =   393216
  98.       CancelError     =   -1  'True
  99.    End
  100.    Begin VB.Label Label2 
  101.       Caption         =   "Frames to load:"
  102.       Height          =   255
  103.       Left            =   120
  104.       TabIndex        =   9
  105.       Top             =   120
  106.       Width           =   1455
  107.    End
  108.    Begin VB.Label Label1 
  109.       Caption         =   "Frames per second:"
  110.       Height          =   255
  111.       Index           =   1
  112.       Left            =   120
  113.       TabIndex        =   4
  114.       Top             =   480
  115.       Width           =   1455
  116.    End
  117.    Begin VB.Label lblResults 
  118.       Height          =   615
  119.       Left            =   120
  120.       TabIndex        =   3
  121.       Top             =   2640
  122.       Width           =   1815
  123.    End
  124.    Begin VB.Menu mnuFile 
  125.       Caption         =   "&File"
  126.       Begin VB.Menu mnuFileOpen 
  127.          Caption         =   "&Open..."
  128.          Shortcut        =   ^O
  129.       End
  130.    End
  131. End
  132. Attribute VB_Name = "frmPlayWait"
  133. Attribute VB_GlobalNameSpace = False
  134. Attribute VB_Creatable = False
  135. Attribute VB_PredeclaredId = True
  136. Attribute VB_Exposed = False
  137. Option Explicit
  138.  
  139. Private NumImages As Integer
  140. Private MaxImage As Integer
  141. Private Playing As Boolean
  142. Private NumPlayed As Long
  143. ' Load the images.
  144. Private Sub LoadImages(file_name As String)
  145. Dim base As String
  146. Dim i As Integer
  147.  
  148.     ' Get the base file name.
  149.     base = Left$(file_name, Len(file_name) - 5)
  150.  
  151.     ' See how many frames the user wants to load.
  152.     If Not IsNumeric(txtNumFrames.Text) Then _
  153.         txtNumFrames.Text = Format$(10)
  154.     NumImages = CInt(txtNumFrames.Text)
  155.  
  156.     ' Create any needed picture boxes.
  157.     For i = MaxImage + 1 To NumImages - 1
  158.         Load picFrame(i)
  159.     Next i
  160.  
  161.     ' Get rid of any that are no longer needed.
  162.     For i = NumImages To MaxImage
  163.         Unload picFrame(i)
  164.     Next i
  165.     MaxImage = NumImages - 1
  166.     
  167.     ' Load the images.
  168.     On Error GoTo LoadPictureError
  169.     i = 0
  170.     Do While i < NumImages
  171.         lblResults.Caption = Format$(i + 1)
  172.         lblResults.Refresh
  173.         picFrame(i).Picture = LoadPicture(base & Format$(i) & ".bmp")
  174.         i = i + 1
  175.     Loop
  176.  
  177.     picCanvas.AutoSize = True
  178.     picCanvas.Picture = picFrame(0).Image
  179.     picCanvas.AutoSize = False
  180.     lblResults.Caption = ""
  181.     txtNumFrames.Text = Format$(NumImages)
  182.     Exit Sub
  183.     
  184. LoadPictureError:
  185.     ' We ran out of images early.
  186.     NumImages = i
  187.     txtNumFrames.Text = Format$(NumImages)
  188.     Resume Next
  189. End Sub
  190.  
  191. ' Run the animation until Playing is false.
  192. Private Sub PlayImages()
  193. Dim ms_per_frame As Integer
  194. Dim start_time As Long
  195. Dim stop_time As Long
  196.  
  197.     ' See how long it should be between frames.
  198.     If Not IsNumeric(txtFramesPerSecond.Text) Then _
  199.         txtFramesPerSecond.Text = "20"
  200.     ms_per_frame = 1000 / CInt(txtFramesPerSecond.Text)
  201.  
  202.     ' Start the appropriate animation.
  203.     NumPlayed = 0
  204.     start_time = GetTickCount
  205.     If optRunType(0).Value Then
  206.         PlayImagesOnce ms_per_frame
  207.     ElseIf optRunType(1).Value Then
  208.         PlayImagesBackAndForth ms_per_frame
  209.     Else
  210.         PlayImagesLooping ms_per_frame
  211.     End If
  212.  
  213.     ' Display results.
  214.     stop_time = GetTickCount
  215.     lblResults.Caption = _
  216.         Format$(NumPlayed) & " frames/" & _
  217.         Format$((stop_time - start_time) / 1000#, "0.00") & _
  218.         " sec" & vbCrLf & vbCrLf & _
  219.         Format$(CSng(NumPlayed) / ((stop_time - start_time) / 1000#), "0.00") & _
  220.         " frames/sec"
  221. End Sub
  222. ' Run the animation forward and backward until
  223. ' Playing is False.
  224. Private Sub PlayImagesBackAndForth(ByVal ms_per_frame As Integer)
  225.     ' Start the animation.
  226.     Do While Playing
  227.         PlayImagesOnce ms_per_frame
  228.         If Not Playing Then Exit Do
  229.         PlayImagesReversed ms_per_frame
  230.     Loop
  231. End Sub
  232. ' Run the animation until Playing is false.
  233. Private Sub PlayImagesLooping(ByVal ms_per_frame As Integer)
  234.     ' Start the animation.
  235.     Do While Playing
  236.         PlayImagesOnce ms_per_frame
  237.     Loop
  238. End Sub
  239. ' Run the animation once or until Playing is False.
  240. Private Sub PlayImagesOnce(ByVal ms_per_frame As Integer)
  241. Dim i As Integer
  242. Dim next_time As Long
  243.  
  244.     ' Get the current time.
  245.     next_time = GetTickCount
  246.  
  247.     ' Start the animation.
  248.     For i = 0 To NumImages - 1
  249.         ' Display the next frame.
  250.         picCanvas.Picture = picFrame(i).Picture
  251.         NumPlayed = NumPlayed + 1
  252.  
  253.         ' Wait till we should display the next frame.
  254.         next_time = next_time + ms_per_frame
  255.         WaitTill next_time
  256.  
  257.         If Not Playing Then Exit For
  258.     Next i
  259. End Sub
  260. ' Run the animation reversed once or until Playing
  261. ' is False.
  262. Private Sub PlayImagesReversed(ByVal ms_per_frame As Integer)
  263. Dim i As Integer
  264. Dim next_time As Long
  265.  
  266.     ' Get the current time.
  267.     next_time = GetTickCount
  268.  
  269.     ' Start the animation.
  270.     For i = NumImages - 1 To 0 Step -1
  271.         ' Display the next frame.
  272.         picCanvas.Picture = picFrame(i).Picture
  273.         NumPlayed = NumPlayed + 1
  274.  
  275.         ' Wait till we should display the next frame.
  276.         next_time = next_time + ms_per_frame
  277.         WaitTill next_time
  278.  
  279.         If Not Playing Then Exit For
  280.     Next i
  281. End Sub
  282.  
  283. ' Start or stop playing.
  284. Private Sub CmdStart_Click()
  285.     If Playing Then
  286.         Playing = False
  287.         cmdStart.Caption = "Stopped"
  288.         cmdStart.Enabled = False
  289.     Else
  290.         cmdStart.Caption = "Stop"
  291.         lblResults.Caption = ""
  292.         DoEvents
  293.         Playing = True
  294.         PlayImages
  295.         Playing = False
  296.         cmdStart.Caption = "Start"
  297.         cmdStart.Enabled = True
  298.     End If
  299. End Sub
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306. Private Sub Form_Load()
  307.     dlgOpenFile.InitDir = App.Path
  308. End Sub
  309.  
  310. ' Load new image files.
  311. Private Sub mnuFileOpen_Click()
  312. Dim file_name As String
  313.  
  314.     ' Let the user select a file.
  315.     On Error Resume Next
  316.     dlgOpenFile.FileName = "*_0.BMP"
  317.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  318.     dlgOpenFile.ShowOpen
  319.     If Err.Number = cdlCancel Then
  320.         Exit Sub
  321.     ElseIf Err.Number <> 0 Then
  322.         Beep
  323.         MsgBox "Error selecting file.", , vbExclamation
  324.         Exit Sub
  325.     End If
  326.     On Error GoTo 0
  327.  
  328.     Screen.MousePointer = vbHourglass
  329.     DoEvents
  330.  
  331.     file_name = Trim$(dlgOpenFile.FileName)
  332.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  333.         - Len(dlgOpenFile.FileTitle) - 1)
  334.     Caption = "PlayWait [" & dlgOpenFile.FileTitle & "]"
  335.  
  336.     ' Load the pictures.
  337.     On Error GoTo LoadError
  338.     LoadImages file_name
  339.     On Error GoTo 0
  340.  
  341.     cmdStart.Enabled = True
  342.     Screen.MousePointer = vbDefault
  343.     Exit Sub
  344.  
  345. LoadError:
  346.     Screen.MousePointer = vbDefault
  347.     MsgBox "Error " & Format$(Err.Number) & _
  348.         " opening file '" & file_name & "'" & vbCrLf & _
  349.         Err.Description
  350. End Sub
  351.